package views {
	import at.geoathome.ui.buttons.TextButton;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;	

	/**
	 * @author wolf
	 */
	public class IntroView extends Sprite{
		
		private var _loader:Loader;
		private var _loader_context:LoaderContext;
		private var _picture:Bitmap;
		private var _text:TextField=new TextField();
		private var _bar:Sprite=new Sprite();
		private var _game:Game;
		private var _startgameBtn:TextButton=new TextButton();
		
		
		public function IntroView(game:Game):void
		{
			
			_game=game;
		}
		public function show() : void
		{
			_loader=new Loader();
			_loader_context = new LoaderContext(true, ApplicationDomain.currentDomain, null);
			_loader.load(new URLRequest("intro_game.png"));
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onAssetLoadComplete);
		}
		private function onAssetLoadComplete(e:Event) : void
		{
			_picture=Bitmap(_loader.content);
			
			addChild(_picture);
			_text.x=15;
			_text.y=580;
			//_text.text="0%";
			_bar.x=0;
			_bar.y=580;		
			addChild(_bar);	
			addChild(_text);
			_startgameBtn.x=0;
			_startgameBtn.y=0;
			_startgameBtn.width=100;
			_startgameBtn.height=30;
			_startgameBtn.addLabel("SKIP INTRO", 0x000000, 14);
			
			_startgameBtn.activated=false;
			addChild(_startgameBtn);
			_startgameBtn.addEventListener(MouseEvent.MOUSE_DOWN, onStart);
			
		}
		
		public function onLoadingSWF(e2:ProgressEvent) : void{
			
			_bar.graphics.beginFill(0xFFFFFF,0.8);
			_bar.graphics.drawRect(0,0,e2.bytesLoaded/e2.bytesTotal*800,20);
			_bar.graphics.endFill();
			var percent:uint=e2.bytesLoaded/e2.bytesTotal*100;
			_text.text=percent+"% loaded";
			trace(String(e2.bytesLoaded/e2.bytesTotal*100));
			if(e2.bytesLoaded==e2.bytesTotal){
				trace("fertig geladen");
				_startgameBtn.activated=true;
				
			}
		}
			
		public function hide():void 
		{
			visible = false;
		}
		
		public function onStart(e:Event) : void{
			_game.changeStateToStartScreen();
			hide();
		}	
	}
}
